﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SMSSendReceiveTool
{
    public partial class sendmsg : Form
    {
        public static sendmsg singletonInstance = null;

        private Form1 OwnerHandle = null;//父窗口的引用

        private sendmsg()
        {
           InitializeComponent();

        }


        public static sendmsg ReturnInstance(Form1 OwnerHandle,string sendNo)
        {
            if (sendmsg.singletonInstance == null)
            {
                sendmsg.singletonInstance = new sendmsg();
                sendmsg.singletonInstance.OwnerHandle = OwnerHandle;
            }
            sendmsg.singletonInstance.textBox1.Text = sendNo;
            if (sendNo.Trim() != string.Empty)
            {
                sendmsg.singletonInstance.textBox1.TabStop = false;
                sendmsg.singletonInstance.textBox2.Focus();
            }
            OwnerHandle.Enabled = false;
            
            return sendmsg.singletonInstance;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (OwnerHandle.gettag == true)
                OwnerHandle.gettag = false;
            sendmsg.singletonInstance = null;
            OwnerHandle.Enabled = true;
            base.OnClosing(e);
        }

        private void button2_Click(object sender, EventArgs e)//取消
        {
            if (OwnerHandle.gettag == true)
                OwnerHandle.gettag = false;
            sendmsg.singletonInstance = null;
            OwnerHandle.Enabled = true;
            
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)//发送短信
        {
            
            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("请输入接收方号码！");
                return;
            }
            
            string centerNo = "13800100500";
            string num = textBox1.Text;
            string context = textBox2.Text;
            PDUencoding pducode = new PDUencoding();
            string length = pducode.CountSMSLength(num, context);
            string smstosendPDU = pducode.PDUEncodingSMS(centerNo,num,context);
            string str = "AT+CMGS=" + length + "\r" + smstosendPDU + "\x01A";//指令格式
            OwnerHandle.SerialPort.Write(str);
            ListViewItem li = new ListViewItem ( ) ;
            li.SubItems.Clear ( ) ;
            li.SubItems[0].Text = DateTime.Now.ToLongDateString()+DateTime.Now.ToLongTimeString() ;
            
            li.SubItems.Add (num) ;
            li.SubItems.Add(context);

            OwnerHandle.ListViewSend.Items.Add(li);
            OwnerHandle.ListViewSend.Items[OwnerHandle.ListViewSend.Items.Count - 1].EnsureVisible();
            if (OwnerHandle.gettag==true)
            {
                OwnerHandle.gettag = false;
                OwnerHandle.listviewreceive.SelectedItems[0].UseItemStyleForSubItems = false;
                OwnerHandle.listviewreceive.SelectedItems[0].SubItems[3].ForeColor = Color.Blue;
                OwnerHandle.listviewreceive.SelectedItems[0].SubItems[3].Text = "已回复";
                //OwnerHandle.huifu.Clear();
                /*for (int i = 0; i < OwnerHandle.listviewreceive.Items.Count; i++)
                {
                    OwnerHandle.huifu.Add(OwnerHandle.listviewreceive.Items[i].SubItems[3].Text);
                }*/
            }
            
            sendmsg.singletonInstance = null;
            OwnerHandle.Enabled = true;

            this.Dispose();
        }
    }
}